package jk.framework.spring.cloud.canary.loadbalancer.comonpent;



import jk.framework.canary.core.CanaryMgt;
import jk.framework.spring.cloud.canary.loadbalancer.constant.CanarySpringCloudConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
import org.springframework.context.ApplicationListener;


/**
 * @author cuichao
 */
public class SpringCloudVersionMetaDataWriterListener implements ApplicationListener<InstancePreRegisteredEvent> {

    private final Logger logger = LoggerFactory.getLogger(SpringCloudVersionMetaDataWriterListener.class);

    @Override
    public void onApplicationEvent(InstancePreRegisteredEvent event) {
        String version = CanaryMgt.getController().localCurrentVersion().getVersion();
        if(logger.isDebugEnabled()){
          logger.debug(String.format("%s 节点向注册中心写入本地版本号: %s",event.getRegistration().getServiceId(),version));
      }
      event.getRegistration()
              .getMetadata()
              .put(CanarySpringCloudConstant.CURRENT_VERSION_KEY,
                      CanaryMgt.getController().localCurrentVersion().getVersion());
    }
}
